<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>Dreamhost Controller by Webdigi UK</title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    	<link rel="stylesheet" type="text/css" href="extjs/resources/css/ext-all.css" />
    	<link rel="stylesheet" type="text/css" href="extjs/resources/css/ext-air.css" />

	    <script type="text/javascript" src="adobe/AIRAliases.js"></script>
		<script type="text/javascript" src="extjs/adapter/ext/ext-base.js"></script>
		<script type="text/javascript" src="extjs/ext-all.js"></script>
		<script type="text/javascript" src="extjs/air/ext-air.js"></script>

		<script>
			var win = new Ext.air.NativeWindow({
			id: 'mainWindow',
			instance: window.nativeWindow,
			minimizeToTray: true,
			trayIcon: 'img/dhlogo.png',
			trayTip: 'Dreamhost API Controller',
			width: 700,
			resizable: false,
			maximizable:false,
			height: 400,
			trayMenu : [{
				text: 'Open Controller',
				handler: function(){
					win.activate();
					}
				}, 
				'-', 
				{
				text: 'Exit',
				handler: function(){
					air.NativeApplication.nativeApplication.exit();
					}
				}]
			});
		</script>
		
		
		
		<script>
			//Global Variables
			//"apitest@dreamhost.com";"6SHU5P2HLDAYECUM";
			var _globaluserloginemail="apitest@dreamhost.com";
			var _globaluserapikey="6SHU5P2HLDAYECUM";
			
Ext.onReady(function(){
   // second tabs built from JS
   try {
   	var tabs2 = new Ext.TabPanel({
   		renderTo: 'bodydiv',
   		activeTab: 0,
   		width: 690,
   		height: 360,
   		plain: true,
   		defaults: {
   			autoScroll: true
   		},
   		items: [{
   			title: 'Home',
   			//html: "My Home Page."
			autoLoad: 'homepage.html',
			listeners: {
				activate: hometabactivate
			}
   		}, {
   			title: 'Announcement',
   			autoLoad: 'announcelist.html',
   			listeners: {
   				activate: announceactivate
   			}
   		}, {
   			title: 'Domains',
   			autoLoad: 'domains.html',
   			//html: "My content was added during construction."
						listeners: {
							activate: domainsactivate
						}
					}, {
						title: 'DNS',
						//autoLoad: {url: 'ajax2.htm', params: 'foo=bar&wtf=1'}
						autoLoad: 'dnslist.html',
						listeners: {
							activate: dnslistactivate
						}
					//html: "My content was added during construction."
					}, {
						title: 'Users',
						autoLoad: 'userlist.html',
						listeners: {
							activate: userlistactivate
						}
					}, {
						title: 'MySQL Users',
						autoLoad: 'mysqluserlist.html',
						listeners: {
							activate: mysqluserlistactivate
						}
					},{
						title: 'Private Servers',
						autoLoad: 'dreamhost.html',
						//disabled:true,
						listeners: {
							activate: handleActivate
						},
						html: "Can't see me cause I'm disabled"
					}, {
						title: 'PS Settings',
						autoLoad: 'pssettings.html',
						//disabled:true,
						listeners: {
							activate: pssettingsActivate
						},
						html: "Can't see me cause I'm disabled"
					}]
				});
			}
			catch(err){
				Ext.Msg.alert('Status', 'Hmmm... Catch');
			}
});
</script>

<script type="text/javascript" language="javascript">
	//global scripts
   function handleActivate()
   {
		sendtoDHAPI('psselect','','','','');
   }
	  
   function resetpslist(selectElement)
   {
	  var elSel = document.getElementById(selectElement);
	  var i;
	  for (i = elSel.length - 1; i>=0; i--) {
			elSel.remove(i);
	  }
   }
   
   function appendOptionLast(selectElement,num)
   {
	  var elOptNew = document.createElement('option');
	  elOptNew.text = num;
	  elOptNew.value = num;
	  var elSel = document.getElementById(selectElement);

	  try {
		elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
	  }
	  catch(ex) {
		elSel.add(elOptNew); // IE only
	  }
   }   
   
   function sendtoDHAPI(toupdate,username,key,cmd,poststr) {

	  finalstr = makeStringDHAPI('','','dreamhost_ps-list_ps','');
	  
	  
	  var connbase = new Ext.data.Connection();
			connbase.request({
				url: 'https://api.dreamhost.com/',
				method: 'POST',
				params: finalstr,
				success: function(responseObject) {
					var resp = eval('('+ responseObject.responseText +')');
					var listps = resp.data;
					if(resp.result!="success")
					{
						Ext.Msg.alert("Error","Please check Login and API privileges");
						return;
					}	
					var firstPsLoad="";
					resetpslist(toupdate);
					//appendOptionLast('');
					for(x in listps)
					{
						var itemNum = parseInt(x);
						if (isNaN(itemNum)) 
							continue; 
						if(listps[itemNum].ps)
						{
							if(firstPsLoad=="")
								firstPsLoad=listps[itemNum].ps;
							appendOptionLast(toupdate,listps[itemNum].ps);
						}
					}
					if(toupdate=="psselect")
						loadps(firstPsLoad);
					if(toupdate=="psselectsettings")
						loadpssettings(firstPsLoad);
				},
				failure: function() {
					Ext.Msg.alert('Status', 'Unable to connect to Dreamhost.');
				 }
			});
   }
   
   function makeStringDHAPI(username,key,cmd,poststr)
   {
	  //var _globaluserloginemail;
	  //var _globaluserapikey;
	  if(username=='')
		username=_globaluserloginemail;
	  if(key=='')
		key=_globaluserapikey;
	  if(cmd=='')
		cmd='dreamhost_ps-list_ps';
	  var finalstr;
	  var uuidstring = "webdigi" + Math.floor(Math.random()*1000001) + "DH" + Math.floor(Math.random()*1000001) + Math.floor(Math.random()*1000001);
	  finalstr = "username=" + username + "&key=" + key + "&cmd=" + cmd + "&unique_id=" + uuidstring + "&format=json" + "&" + poststr ;
	  return finalstr;
   }
   
   function loadps(psStr)
   {
		var addstr = ""
		if(psStr!="")
		{
			addstr = "ps="+psStr;
									
			postStr2 = makeStringDHAPI('','','dreamhost_ps-list_usage',addstr);
			var conn2 = new Ext.data.Connection();
			conn2.request({
				url: 'https://api.dreamhost.com/',
				method: 'POST',
				params: postStr2,
				success: function(responseObject) {
					var resp = eval('('+ responseObject.responseText +')');
					document.getElementById('graphpro1').innerHTML='';					
					if(resp.result!="success")
					{
						Ext.Msg.alert("Error","Please check Login and API privileges");
						return;
					}						
					var graphList = resp.data;
					var procList="", memList="";
					for(x in graphList)
					{
						var itemNum = parseInt(x);
						if (isNaN(itemNum)) 
							continue; 
						if(graphList[itemNum].memory_mb)
						{
							if(memList!="")
								memList = memList + "," + graphList[itemNum].memory_mb ;
							else
								memList = graphList[itemNum].memory_mb ;
						}
						if(graphList[itemNum].load)
						{
							if(procList!="")
								procList = procList + "," + Math.round((graphList[itemNum].load * 100));
							else
								procList = Math.round((graphList[itemNum].load * 100));
						}
					}
					var urlchart = "&nbsp;&nbsp;<img src='http://chart.apis.google.com/chart?cht=lc&chd=t:" + memList + "&chtt=Memory&chs=300x200&chco=336699,990000'></img>";
					urlchart = urlchart + "&nbsp;&nbsp;&nbsp;&nbsp;<img src='http://chart.apis.google.com/chart?cht=lc&chd=t:" + procList + "&chtt=Load&chs=300x200&chco=336699,990000'></img>";
					document.getElementById('graphpro1').innerHTML=urlchart;					
				},
				failure: function() {
				 Ext.Msg.alert('Status', 'Unable to connect to Dreamhost.');
				 }
			});
		}
   }
   
   function loadpssettings(psStr){
		var addstr = ""
		if (psStr != "") {
			addstr = "ps=" + psStr;
			
			postStr1 = makeStringDHAPI('', '', 'dreamhost_ps-list_settings', addstr);
			var conn1 = new Ext.data.Connection();
			conn1.request({
				url: 'https://api.dreamhost.com/',
				method: 'POST',
				params: postStr1,
				success: function(responseObject){
					var resp = eval('(' + responseObject.responseText + ')');
					var pssetdata = resp.data;
					if(resp.result!="success")
					{
						Ext.Msg.alert("Error","Please check Login and API privileges");
						return;
					}	
					document.getElementById('pssettings').innerHTML="";
					var setarr = [];
					for(x in pssetdata)
					{
						var itemNum = parseInt(x);
						if (isNaN(itemNum)) 
							continue; 
						setarr[pssetdata[itemNum].setting]=pssetdata[itemNum].value;	
					}
					//alert('pssettings');
					var myForm = new Ext.form.FormPanel({
						renderTo: "pssettings",
						//title: "PS Settings",
						width: 425,
						frame: true,
						items: [{
							xtype: "textfield",
							fieldLabel: "Comment",
							name: "commentbox",
							value: setarr["comment"]
						}, {
							xtype: "checkbox",
							fieldLabel: "Apache2",
							name: "checkbox",
							inputValue: "cbvalue",
							checked: setarr["apache2_enabled"]
						}, {
							xtype: "checkbox",
							fieldLabel: "Courier",
							name: "checkbox",
							inputValue: "cbvalue",
							checked: setarr["courier_enabled"]
						}, {
							xtype: "checkbox",
							fieldLabel: "Jabber",
							name: "checkbox",
							inputValue: "cbvalue",
							checked: setarr["jabber_transports_enabled"]
						}, {
							xtype: "checkbox",
							fieldLabel: "Lighttpd",
							name: "checkbox",
							inputValue: "cbvalue",
							checked: setarr["lighttpd_enabled"]
						}, {
							xtype: "checkbox",
							fieldLabel: "modphp_4",
							name: "checkbox",
							inputValue: "cbvalue",
							checked: setarr["modphp_4_selected"]
						}, {
							xtype: "checkbox",
							fieldLabel: "PHP X-cache",
							name: "checkbox",
							inputValue: "cbvalue",
							checked: setarr["modphp_4_selected"]
						}, {
							xtype: "checkbox",
							fieldLabel: "ProFtpd",
							name: "checkbox",
							inputValue: "cbvalue",
							checked: setarr["proftpd_enabled"]
						}],
						buttons: [{
							text: "Cancel"
						}, {
							text: "Save",
							handler: function(){
								Ext.Msg.alert("Not implemented","Save function not yet implemented in Beta");
							}
						}],
						tbar:[
							new Ext.Button({text: "Reboot Server", icon:"img/restart.png", cls:"x-btn-text-icon", handler: function(){
								var rebootserverconf = "You are about to reboot " + setarr["machine"];
								Ext.Msg.show({
								   title:'Please Confirm',
								   msg: rebootserverconf,
								   buttons: Ext.Msg.YESNO,
								   fn: function(btn,ext){
								   	if(btn=='yes')
								   	{
										//alert('rebooting PS'+setarr["machine"]);
										postStr2 = makeStringDHAPI('','','dreamhost_ps-reboot','&ps='+setarr["machine"]);
										var conn2 = new Ext.data.Connection();
										conn2.request({	
											url: 'https://api.dreamhost.com/',
											method: 'POST',
											params: postStr2,
											success: function(responseObject) {
												var resp = eval('('+ responseObject.responseText +')');
												if(resp.result=="success")
												{
													Ext.Msg.alert('Success',"Reboot has been scheduled");
												}
												else{
													Ext.Msg.alert('Failure',"Reboot not successful");
												}
											},
											failure: function() {
												 Ext.Msg.alert('Status', 'Unable to connect to Dreamhost.');
											 }
										});
									}	
								   }
								});
								//Ext.Msg.alert(setarr["machine"]);
							}})
						]
					});
				},
				failure: function(){
					Ext.Msg.alert('Status', 'Unable to connect to Dreamhost.');
				}
			});
		}
   }
   function domainsactivate()
   {
	  finalstr = makeStringDHAPI('','','domain-list_domains','');
	  
	  
	  var connbase = new Ext.data.Connection();
			connbase.request({
				url: 'https://api.dreamhost.com/',
				method: 'POST',
				params: finalstr,
				success: function(responseObject) {
					var resp = eval('('+ responseObject.responseText +')');
					var listps = resp.data;
					var firstPsLoad="";
					if(resp.result!="success")
					{
						Ext.Msg.alert("Error","Please check Login and API privileges");
						return;
					}	

					var domdata = resp.data;
					var setarr = [];
					for(x in domdata)
					{
						var itemNum = parseInt(x);
						if (isNaN(itemNum)) 
							continue; 
						var detailarray = new Array(domdata[itemNum].domain,domdata[itemNum].hosting_type,domdata[itemNum].user,domdata[itemNum].unique_ip,domdata[itemNum].home); 
						setarr[x]=detailarray;	
					}

					document.getElementById('domainsdivholder').innerHTML="";
					var sampleData = setarr;
	
					// 2. Create the Store
					var store = new Ext.data.SimpleStore({ 
					fields : [
   { name: 'Domain', type: 'string' },
   { name: 'Hosting', type: 'string' },
   { name: 'User', type: 'string' },
   { name: 'IP', type: 'string' },
   { name: 'Home', type: 'string' }
  ],
  data: sampleData
 });

 // 3. Initialize the grid control
 var simpleGrid = new Ext.grid.GridPanel({
  store: store,
		columns: [{
		  header: 'Domain', 
			 width: 160, 
			 sortable: true, 
			 dataIndex: 'Domain'
			},
			{
			 header: 'Hosting', 
			 width: 75, 
			 sortable: true, 
			 dataIndex: 'Hosting'
			},
			{
			 header: 'User',
			 width: 75, 
			 sortable: true, 
			 dataIndex: 'User'
			},
			{
			 header: 'IP',
			 width: 95, 
			 sortable: true, 
			 dataIndex: 'IP' 
			},
			{
			 header: 'Home',
			 width: 165, 
			 sortable: true, 
			 dataIndex: 'Home' 
			}
		],
		//clicksToEdit: 1,
		stripeRows: true,
		autoHeight:true,
		//width:500,
		enableHdMenu: false, // 7. Disable the context menu
		//title:'Domain List',
		renderTo: 'domainsdivholder',
		tbar:[
			new Ext.Button({text: "Refresh", handler: domainsactivate, icon:"img/refresh.png", cls:"x-btn-text-icon",})
		]
 });

					
				},
				failure: function() {
					Ext.Msg.alert('Status', 'Unable to connect to Dreamhost.');
				 }
			});
   }
   
   function dnslistactivate()
   {
	  finalstr = makeStringDHAPI('','','dns-list_records','');
	  
	  
	  var connbase = new Ext.data.Connection();
			connbase.request({		
				url: 'https://api.dreamhost.com/',
				method: 'POST',
				params: finalstr,
				success: function(responseObject) {
					
					var dnsaddwin;
					var resp = eval('('+ responseObject.responseText +')');
					var dnsdata = resp.data;
					var setarr = [];
					if(resp.result!="success")
					{
						Ext.Msg.alert("Error","Please check Login and API privileges");
						return;
					}	

					for(x in dnsdata)
					{
						var itemNum = parseInt(x);
						if (isNaN(itemNum)) 
							continue; 
						var detailarray = new Array(dnsdata[itemNum].type,dnsdata[itemNum].record,dnsdata[itemNum].zone,dnsdata[itemNum].value,dnsdata[itemNum].comment); 
						setarr[x]=detailarray;	
					}

					document.getElementById('dnslistdivholder').innerHTML="";
					var sampleData = setarr;
	
					// 2. Create the Store
					var store = new Ext.data.SimpleStore({ 
					fields : [
   { name: 'type', type: 'string' },
   { name: 'record', type: 'string' },
   { name: 'zone', type: 'string' },
   { name: 'value', type: 'string' },
   { name: 'comment', type: 'string' }
  ],
  data: sampleData
 });

 // 3. Initialize the grid control
 var simpleGrid = new Ext.grid.GridPanel({
  		store: store,
		columns: [{
		  header: 'Type', 
			 width: 50, 
			 sortable: true, 
			 dataIndex: 'type'
			},
			{
			 header: 'Record', 
			 width: 120, 
			 sortable: true, 
			 dataIndex: 'record'
			},
			{
			 header: 'Value',
			 width: 155, 
			 sortable: true, 
			 dataIndex: 'value' 
			},
			{
			 header: 'Comment',
			 width: 165, 
			 sortable: true, 
			 dataIndex: 'comment' 
			}
		],
		//clicksToEdit: 1,
		stripeRows: true,
		autoHeight:true,
		//width:500,
		enableHdMenu: false, // 7. Disable the context menu
		//title:'Domain List',
		renderTo: 'dnslistdivholder',
		tbar:[
				new Ext.Button({text: "Refresh", handler: dnslistactivate, icon:"img/refresh.png", cls:"x-btn-text-icon",}),

				new Ext.Toolbar.Button({text: "Add DNS Entry", icon:"extjs/resources/images/default/dd/drop-add.gif", cls:"x-btn-text-icon", handler: function(){
				// create the window on the first click and reuse on subsequent clicks
		        if(!dnsaddwin){
					document.getElementById('dnsadd-win').innerHTML="";
					var mydnsForm = new Ext.form.FormPanel({
						width: 425,
						items: [{
							xtype: "textfield",
							fieldLabel: "Record",
							name: "recordbox"
							},{
							xtype: "textfield",
							fieldLabel: "Value",
							name: "valuecombobox"
							},{
							xtype: "textfield",
							fieldLabel: "Type",
							name: "typecombobox"
							},{
							xtype: "textfield",
							fieldLabel: "Comment",
							name: "commentcombobox"
							}]
						});
	        	    dnsaddwin = new Ext.Window({
	    	            applyTo     : 'dnsadd-win',
		                layout      : 'fit',
	                	width       : 400,
    	        	    height      : 200,
        		        closeAction : 'hide',
    	    	        plain       : true,
	            	    items       : mydnsForm,

	            	    buttons: [{
    	    	            text     : 'Submit',
    		                waitMsg: 'Sending Dreamhost the request...',
							handler  : function(){
								var recordboxel = document.getElementsByName('recordbox');
								var valuecomboboxel = document.getElementsByName('valuecombobox');
								var typecomboboxel = document.getElementsByName('typecombobox');
								var commentcomboboxel = document.getElementsByName('commentcombobox');
								var dnsparam = "&record="+recordboxel[0].value+"&type="+typecomboboxel[0].value+"&value="+valuecomboboxel[0].value+"&comment="+commentcomboboxel[0].value+"&";
								//Ext.Msg.alert("DNS",dnsparam);
								
								finalstr = makeStringDHAPI('','','dns-add_record',dnsparam);
	  
	  
	  var connbase = new Ext.data.Connection();
			connbase.request({
				url: 'https://api.dreamhost.com/',
				method: 'POST',
				params: finalstr,
				success: function(responseObject) {
					var resp = eval('('+ responseObject.responseText +')');
					var listps = resp.data;
					if(resp.result!="success")
					{
						Ext.Msg.alert("Error","API Error : " + resp.data);
						return;
					}
					else
					{
						Ext.Msg.alert("Confirmation","DNS Entry was successful, Please refresh");
					}	
				},
				failure: function() {
					Ext.Msg.alert('Status', 'Unable to connect to Dreamhost.');
				 }
			});

								
							}
	    	    	        },{
                	    	text     : 'Close',
                		    handler  : function(){
            	        	    dnsaddwin.hide();
	        	            }
    		            }
						]
	    	        });
	        	}
		        dnsaddwin.show(this);
					}})
				]
			});
			
				},
				failure: function() {
					Ext.Msg.alert('Status', 'Unable to connect to Dreamhost.');
				 }
			});   	
   }
   
    //mysqluserlistdivholder
	function mysqluserlistactivate(){
		
	  finalstr = makeStringDHAPI('','','mysql-list_users','');
	  
	  
	  var connbase = new Ext.data.Connection();
			connbase.request({		
				url: 'https://api.dreamhost.com/',
				method: 'POST',
				params: finalstr,
				success: function(responseObject) {
					var resp = eval('('+ responseObject.responseText +')');
					var userdata = resp.data;
					var setarr = [];
					if(resp.result!="success")
					{
						Ext.Msg.alert("Error","Please check Login and API privileges");
						return;
					}	

					for(x in userdata)
					{
						var itemNum = parseInt(x);
						if (isNaN(itemNum)) 
							continue; 
						var detailarray = new Array(userdata[itemNum].account_id,userdata[itemNum].db,userdata[itemNum].home,userdata[itemNum].username,userdata[itemNum].host,userdata[itemNum].select_priv,userdata[itemNum].insert_priv,userdata[itemNum].update_priv,userdata[itemNum].delete_priv,userdata[itemNum].create_priv,userdata[itemNum].drop_priv,userdata[itemNum].index_priv); 
						setarr[x]=detailarray;	
					}

					document.getElementById('mysqluserlistdivholder').innerHTML="";
					var sampleData = setarr;
	
					// 2. Create the Store
					var store = new Ext.data.SimpleStore({ 
					fields : [
   { name: 'account_id', type: 'string' },
   { name: 'db', type: 'string' },
   { name: 'home', type: 'string' },
   { name: 'username', type: 'string' },
   { name: 'host', type: 'string'},
   { name: 'select_priv', type: 'string'},
   { name: 'insert_priv', type: 'string'},
   { name: 'update_priv', type: 'string'},
   { name: 'delete_priv', type: 'string'},
   { name: 'create_priv', type: 'string'},   
   { name: 'drop_priv', type: 'string'},
   { name: 'index_priv', type: 'string'}
  ],
  data: sampleData
 });
 
 // 3. Initialize the grid control
 var simpleGrid = new Ext.grid.GridPanel({
		store: store,
		columns: [
			{
			 header: 'Database', 
			 width: 140, 
			 sortable: true, 
			 dataIndex: 'db'
			},
			{
			 header: 'Home',
			 width: 155, 
			 sortable: true, 
			 dataIndex: 'home' 
			},
			{
			 header: 'Username',
			 width: 125, 
			 sortable: true, 
			 dataIndex: 'username' 
			},
			{
			 header: 'Sel',
			 width: 40, 
			 sortable: true, 
			 dataIndex: 'select_priv' 
			},
			{
			 header: 'Ins',
			 width: 40, 
			 sortable: true, 
			 dataIndex: 'insert_priv' 
			},
			{
			 header: 'Upd',
			 width: 40, 
			 sortable: true, 
			 dataIndex: 'update_priv' 
			},
			{
			 header: 'Del',
			 width: 40, 
			 sortable: true, 
			 dataIndex: 'delete_priv' 
			},
			{
			 header: 'Cre',
			 width: 40, 
			 sortable: true, 
			 dataIndex: 'create_priv' 
			},
			{
			 header: 'Drop',
			 width: 40, 
			 sortable: true, 
			 dataIndex: 'drop_priv' 
			}
		],
		//clicksToEdit: 1,
		stripeRows: true,
		autoHeight:true,
		//width:500,
		enableHdMenu: false, // 7. Disable the context menu
		//title:'Domain List',
		renderTo: 'mysqluserlistdivholder',
		tbar:[
				new Ext.Button({text: "Refresh", handler: mysqluserlistactivate, icon:"img/refresh.png", cls:"x-btn-text-icon",})
			]
 });				
				},
				failure: function() {
					Ext.Msg.alert('Status', 'Unable to connect to Dreamhost.');
				 }
			});   		
		
	}

	function userlistactivate(){
		
	  finalstr = makeStringDHAPI('','','user-list_users_no_pw','');
	  
	  
	  var connbase = new Ext.data.Connection();
			connbase.request({		
				url: 'https://api.dreamhost.com/',
				method: 'POST',
				params: finalstr,
				success: function(responseObject) {
					var resp = eval('('+ responseObject.responseText +')');
					var userdata = resp.data;
					var setarr = [];
					if(resp.result!="success")
					{
						Ext.Msg.alert("Error","Please check Login and API privileges");
						return;
					}	

					for(x in userdata)
					{
						var itemNum = parseInt(x);
						if (isNaN(itemNum)) 
							continue; 
						var detailarray = new Array(userdata[itemNum].type,userdata[itemNum].username,userdata[itemNum].shell,userdata[itemNum].home,userdata[itemNum].disk_used_mb,userdata[itemNum].gecos); 
						setarr[x]=detailarray;	
					}

					document.getElementById('userlistdivholder').innerHTML="";
					var sampleData = setarr;
	
					// 2. Create the Store
					var store = new Ext.data.SimpleStore({ 
					fields : [
   { name: 'type', type: 'string' },
   { name: 'username', type: 'string' },
   { name: 'shell', type: 'string' },
   { name: 'home', type: 'string' },
   { name: 'disk_used_mb', type: 'float'},
   { name: 'gecos', type: 'string' } 
  ],
  data: sampleData
 });
 
 // 3. Initialize the grid control
 var simpleGrid = new Ext.grid.GridPanel({
		store: store,
		columns: [{
		  	 header: 'Type', 
			 width: 50, 
			 sortable: true, 
			 dataIndex: 'type'
			},
			{
			 header: 'Username', 
			 width: 80, 
			 sortable: true, 
			 dataIndex: 'username'
			},
			{
			 header: 'Shell',
			 width: 155, 
			 sortable: true, 
			 dataIndex: 'shell' 
			},
			{
			 header: 'Home',
			 width: 165, 
			 sortable: true, 
			 dataIndex: 'home' 
			},
			{
			 header: 'Disk',
			 width: 80, 
			 sortable: true, 
			 dataIndex: 'disk_used_mb' 
			},
			{
			 header: 'Comment',
			 width: 165, 
			 sortable: true, 
			 dataIndex: 'gecos' 
			}
		],
		//clicksToEdit: 1,
		stripeRows: true,
		autoHeight:true,
		//width:500,
		enableHdMenu: false, // 7. Disable the context menu
		//title:'Domain List',
		renderTo: 'userlistdivholder',
		tbar:[
				new Ext.Button({text: "Refresh", handler: userlistactivate, icon:"img/refresh.png", cls:"x-btn-text-icon",})
			]
 });				
				},
				failure: function() {
					Ext.Msg.alert('Status', 'Unable to connect to Dreamhost.');
				 }
			});   		
		
	}
	
	function announceactivate(){
		
	  finalstr = makeStringDHAPI('','','announcement_list-list_lists','');
	  
	  
	  var connbase = new Ext.data.Connection();
			connbase.request({		
				url: 'https://api.dreamhost.com/',
				method: 'POST',
				params: finalstr,
				success: function(responseObject) {
					var resp = eval('('+ responseObject.responseText +')');
					if(resp.result!="success")
					{
						Ext.Msg.alert("Error","Please check Login and API privileges");
						return;
					}	
					var userdata = resp.data;
					var setarr = [];
					for(x in userdata)
					{
						var itemNum = parseInt(x);
						if (isNaN(itemNum)) 
							continue;
						var datechange =  userdata[itemNum].start_date.split("-");
						var sdatechange = datechange[2] + '-' + datechange[1] + '-' + datechange[0];
						var detailarray = new Array(userdata[itemNum].listname,userdata[itemNum].domain,userdata[itemNum].name,sdatechange,userdata[itemNum].num_subscribers); 
						setarr[x]=detailarray;	
					}

					document.getElementById('announcedivholder').innerHTML="";
					var sampleData = setarr;
	
					// 2. Create the Store
					var store = new Ext.data.SimpleStore({ 
					fields : [
   { name: 'listname', type: 'string' },
   { name: 'domain', type: 'string' },
   { name: 'name', type: 'string' },
   { name: 'start_date', type: 'string' },
   { name: 'num_subscribers', type: 'int'}
  ],
  data: sampleData
 });

 // 3. Initialize the grid control
 var simpleGrid = new Ext.grid.GridPanel({
		store: store,
		columns: [{
		  header: 'List', 
			 width: 100, 
			 sortable: true, 
			 dataIndex: 'listname'
			},
			{
			 header: 'Domain', 
			 width: 80, 
			 sortable: true, 
			 dataIndex: 'domain'
			},
			{
			 header: 'Name',
			 width: 105, 
			 sortable: true, 
			 dataIndex: 'name' 
			},
			{
			 header: 'Start Date',
			 width: 105, 
			 sortable: true, 
			 dataIndex: 'start_date' 
			},
			{
			 header: 'Subscribers',
			 width: 80, 
			 sortable: true, 
			 dataIndex: 'num_subscribers' 
			}
		],
		//clicksToEdit: 1,
		stripeRows: true,
		autoHeight:true,
		//width:500,
		enableHdMenu: false, // 7. Disable the context menu
		//title:'Domain List',
		renderTo: 'announcedivholder',
		tbar:[
				new Ext.Button({text: "Refresh", handler: announceactivate, icon:"img/refresh.png", cls:"x-btn-text-icon",})
			]
 });

					
				},
				failure: function() {
					Ext.Msg.alert('Status', 'Unable to connect to Dreamhost.');
				 }
			});   				
	}
	
	function hometabactivate(){
		//window.setTimeout("document.getElementById('homepageloginbtn').innerHTML='Hi';",1000);
		//setTimeout(homepage_loginwin,1000);
		
	  finalstr = makeStringDHAPI('','','api-list_accessible_cmds','');	  
	  
	  var connbase = new Ext.data.Connection();
			connbase.request({
				url: 'https://api.dreamhost.com/',
				method: 'POST',
				params: finalstr,
				success: function(responseObject) {
					var resp = eval('('+ responseObject.responseText +')');
					if(resp.data=="auth_error" && resp.result=="error")
					{
						homepage_loginwin();
						Ext.Msg.alert("Authentication","Failed to login via API");
					}
					else{
						document.getElementById('homepagediv').innerHTML="";
						var homepanelmsg = '';
						homepanelmsg = 'Successful connection established to dreamhost API using <b>'+ _globaluserloginemail + "</b><br /><br /><b>Accessible APIs</b><br /><ul>";
						var userdata = resp.data;
						for(x in userdata)
						{
							var itemNum = parseInt(x);
							if (isNaN(itemNum)) 
								continue;
							homepanelmsg = homepanelmsg + "<li>&nbsp;&nbsp;&nbsp;" + userdata[itemNum].cmd + "</li>";
						}
						homepanelmsg = homepanelmsg + "</ul>";					
						
						var p = new Ext.Panel({
					        title: 'API Details',
					        collapsible:true,
					        renderTo: 'homepagediv',
					        //width:400,
					        html: homepanelmsg,
							tbar: new Ext.Toolbar.Button({text: "Change Login", icon:"extjs/resources/images/default/dd/drop-add.gif", cls:"x-btn-text-icon", handler:homepage_loginwin})
					    });
					}
				},
				failure: function() {
					Ext.Msg.alert('Status', 'Unable to connect to Dreamhost.');
				 }
			});	
	}
	var homeloginwin;
	function homepage_loginwin(){
		//Ext.Msg.alert('One','Message from heaven');	
		if(!homeloginwin){

			var myloginForm = new Ext.form.FormPanel({
				//width: 425,
				items: [{
						xtype: "textfield",
						fieldLabel: "Email",
						name: "emailbox"
					},{
						xtype: "textfield",
						fieldLabel: "API Key",
						name: "apikeybox"
					}]
						});			
	        homeloginwin = new Ext.Window({
	    	            applyTo     : 'homelogin-win',
		                layout      : 'fit',
	                	width       : 300,
    	        	    height      : 150,
        		        closeAction : 'hide',
    	    	        plain       : true,
	            	    items       : myloginForm,

	            	    buttons: [{
    	    	            text     : 'Submit',
    		                waitMsg: 'Sending Dreamhost the request...',
							handler  : function(){
							  
							  var emailboxel = document.getElementsByName('emailbox');
							  var apikeyboxel = document.getElementsByName('apikeybox');
																
							  finalstr = makeStringDHAPI(emailboxel[0].value,apikeyboxel[0].value,'api-list_accessible_cmds','');
							  var connbase = new Ext.data.Connection();
							  connbase.request({
								url: 'https://api.dreamhost.com/',
								method: 'POST',
								params: finalstr,
								success: function(responseObject) {
									var resp = eval('('+ responseObject.responseText +')');
									if(resp.result=="success")
									{
										_globaluserloginemail = emailboxel[0].value;
										_globaluserapikey = apikeyboxel[0].value;
										homeloginwin.hide();
										hometabactivate();
									}
									else{
										Ext.Msg.alert('Authentication',"Unable to authenticate");
									}
								},
								failure: function() {
									Ext.Msg.alert('Status', 'Unable to connect to Dreamhost.');
								 }
						});			
								
							}
							//disabled : true
	    	    	        },{
                	    	text     : 'Close',
                		    handler  : function(){
            	        	    homeloginwin.hide();
	        	            }
    		            }
						]
	    	        });
					homeloginwin.show(this);
	        	}
				else 
			        homeloginwin.show(this);
	}
	
	//pssettings.html	
	function pssettingsActivate()
	{
		sendtoDHAPI('psselectsettings','','','','');
	}
</script>
		
		
	</head>
	<body>
		<div id='bodydiv'></div>
	</body>
</html>
